package org.example.everyday;

import java.util.*;

public class Lc954 {
    public static void main(String[] args) {
        Lc954 lc954 = new Lc954();
//        int[] nums = new int[]{2,1,2,6};
        //[-2,-4,2,4]
        int[] nums = new int[]{4,-2,2,-4};
        boolean result = lc954.canRecordDoubled(nums);
        System.out.println(result);
    }


    public boolean canRecordDoubled(int[] arr){
        Map<Integer,Integer> cnt = new HashMap<Integer,Integer>();
        for(int x : arr){
            cnt.put(x,cnt.getOrDefault(x,0) + 1);
        }
        if(cnt.getOrDefault(0,0) % 2 != 0){
            return false;
        }

        List<Integer> vals = new ArrayList<Integer>();
        for(int x : cnt.keySet()){
            vals.add(x);
        }

        Collections.sort(vals,(a,b) -> Math.abs(a) - Math.abs(b));

        for(int x : vals){
            if(cnt.getOrDefault(2 * x,0) < cnt.get(x)){
                return false;
            }
            cnt.put(2 * x, cnt.getOrDefault(2 * x,0) - cnt.get(x));
        }
        return true;
    }

}
